﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class SignalChartFrm : System.Windows.Forms.Form
    {
        // 통합 신호 관리자 생성
        EasyTrader.Signal.TotalSignalManager m_GSignalManager = null;
        public EasyTrader.Signal.TotalSignalManager GSignalManager
        {
            get { return m_GSignalManager; }
            set { m_GSignalManager = value; }
        }

        public SignalChartFrm()
        {
            InitializeComponent();
        }

        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = GlobalVar.ChartRefreshInterval;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        private void InitControl()
        {
            SignalChartCtrl.Series["코스피외국인개인"].ToolTip = "#VAL";
            SignalChartCtrl.Series["코스피기관개인"].ToolTip = "#VAL";
            SignalChartCtrl.Series["선물외국인"].ToolTip = "#VAL";
            SignalChartCtrl.Series["옵션개인콜풋"].ToolTip = "#VAL";
            SignalChartCtrl.Series["코스피프로그램"].ToolTip = "#VAL";
            SignalChartCtrl.Series["호가잔량"].ToolTip = "#VAL";
            SignalChartCtrl.Series["호가건수"].ToolTip = "#VAL";
            SignalChartCtrl.Series["파동"].ToolTip = "#VAL";
            SignalChartCtrl.Series["예측지수"].ToolTip = "#VAL";
            SignalChartCtrl.Series["전체"].ToolTip = "#VAL";
            SignalChartCtrl.Series["코스피외국인"].ToolTip = "#VAL";
            SignalChartCtrl.Series["코스피기관"].ToolTip = "#VAL";
            SignalChartCtrl.Series["전체"]["PointWidth"] = (1.0).ToString();
        }
        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            // 차트에 데이터를 대응시켜 준다.
            SetChartData();
        }

        private void SetChartData()
        {
            if (m_GSignalManager == null || GlobalVar.AllDataDownloaded == false)
                return;
            
            SignalChartCtrl.Series["코스피외국인개인"].Points.Clear();
            SignalChartCtrl.Series["코스피기관개인"].Points.Clear();
            SignalChartCtrl.Series["선물외국인"].Points.Clear();
            SignalChartCtrl.Series["옵션개인콜풋"].Points.Clear();
            SignalChartCtrl.Series["코스피프로그램"].Points.Clear();
            SignalChartCtrl.Series["호가잔량"].Points.Clear();
            SignalChartCtrl.Series["호가건수"].Points.Clear();
            SignalChartCtrl.Series["파동"].Points.Clear();
            SignalChartCtrl.Series["예측지수"].Points.Clear();
            SignalChartCtrl.Series["전체"].Points.Clear();
            SignalChartCtrl.Series["코스피외국인"].Points.Clear();
            SignalChartCtrl.Series["코스피기관"].Points.Clear();

            int dayIndex = 1;
            string valueText = "";
            if (m_GSignalManager.ForeignPersonKospiSignal > 0.0)
            {
                SignalChartCtrl.Series["코스피외국인개인"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["코스피외국인개인"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["코스피외국인개인"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.ForeignPersonKospiSignal));
            SignalChartCtrl.Series["코스피외국인개인"].Points[0].AxisLabel = GlobalVar.TimeToIntEx().ToString();
            SignalChartCtrl.Series["코스피외국인개인"].Label = "코스피외국인개인 " + valueText + m_GSignalManager.ForeignPersonKospiSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.OrganPersonKospiSignal > 0.0)
            {
                SignalChartCtrl.Series["코스피기관개인"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["코스피기관개인"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["코스피기관개인"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.OrganPersonKospiSignal));
            SignalChartCtrl.Series["코스피기관개인"].Label = "코스피기관개인 " + valueText + m_GSignalManager.OrganPersonKospiSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.FutureSignal > 0.0)
            {
                SignalChartCtrl.Series["선물외국인"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["선물외국인"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["선물외국인"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.FutureSignal));
            SignalChartCtrl.Series["선물외국인"].Label = "선물외국인 " + valueText + m_GSignalManager.FutureSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.OptionCallPutSignal > 0.0)
            {
                SignalChartCtrl.Series["옵션개인콜풋"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["옵션개인콜풋"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["옵션개인콜풋"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.OptionCallPutSignal));
            SignalChartCtrl.Series["옵션개인콜풋"].Label = "옵션개인콜풋 " + valueText + m_GSignalManager.OptionCallPutSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.ProgramSignal > 0.0)
            {
                SignalChartCtrl.Series["코스피프로그램"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["코스피프로그램"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["코스피프로그램"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.ProgramSignal));
            SignalChartCtrl.Series["코스피프로그램"].Label = "코스피프로그램 " + valueText + m_GSignalManager.ProgramSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////

            if (m_GSignalManager.BidsAsksSignal > 0.0)
            {
                SignalChartCtrl.Series["호가잔량"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["호가잔량"].Color = Color.Blue;
                valueText = "매도 : ";
            }


            SignalChartCtrl.Series["호가잔량"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.BidsAsksSignal));
            SignalChartCtrl.Series["호가잔량"].Label = "호가잔량 " + valueText + m_GSignalManager.BidsAsksSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////

            if (m_GSignalManager.BuySellSignal > 0.0)
            {
                SignalChartCtrl.Series["호가건수"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["호가건수"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["호가건수"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.BuySellSignal));
            SignalChartCtrl.Series["호가건수"].Label = "호가건수 " + valueText + m_GSignalManager.BuySellSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.WaveSignal > 0.0)
            {
                SignalChartCtrl.Series["파동"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["파동"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["파동"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.WaveSignal));
            SignalChartCtrl.Series["파동"].Label = "파동 " + valueText + m_GSignalManager.WaveSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.ForeSignal > 0.0)
            {
                SignalChartCtrl.Series["예측지수"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["예측지수"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["예측지수"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.ForeSignal));
            SignalChartCtrl.Series["예측지수"].Label = "예측지수 " + valueText + m_GSignalManager.ForeSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.HighLowSignal > 0.0)
            {
                SignalChartCtrl.Series["고저비율"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["고저비율"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["고저비율"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.HighLowSignal));
            SignalChartCtrl.Series["고저비율"].Label = "고저비율" + valueText + m_GSignalManager.HighLowSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.WorldSignal > 0.0)
            {
                SignalChartCtrl.Series["해외지수"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["해외지수"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["해외지수"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.WorldSignal));
            SignalChartCtrl.Series["해외지수"].Label = "해외지수" + valueText + m_GSignalManager.WorldSignal.ToString("F2");
            //////////////////////////////////////////////////////////////////////////
            if (m_GSignalManager.TotalSignal > 0.0)
            {
                SignalChartCtrl.Series["전체"].Color = Color.Red;
                valueText = "매수 : ";
            }
            else
            {
                SignalChartCtrl.Series["전체"].Color = Color.Blue;
                valueText = "매도 : ";
            }

            SignalChartCtrl.Series["전체"].Points.AddXY(dayIndex, Math.Abs(m_GSignalManager.TotalSignal));
            SignalChartCtrl.Series["전체"].Label = "전체 " + valueText + m_GSignalManager.TotalSignal.ToString("F2");
            SignalChartCtrl.ChartAreas["Price"].AxisY.Minimum = 0;
            SignalChartCtrl.ChartAreas["Price"].AxisY.Maximum = 100;

            EasyTrader.Signal.ForecastingInfo foreignInfo = m_GSignalManager.KospiForeignInfo;
            EasyTrader.Signal.ForecastingInfo organInfo = m_GSignalManager.KospiOrganInfo;
            double deltaForeign = foreignInfo.EndY - foreignInfo.StartY;
            double deltaOrgan = organInfo.EndY - organInfo.StartY;
            double totalVal = 0.0;
            
            if (foreignInfo.EndY > 0)
            {
                if (organInfo.EndY > 0)
                {
                    totalVal = foreignInfo.EndY + organInfo.EndY;
                    SignalChartCtrl.Series["코스피외국인"].Points.AddXY(dayIndex, foreignInfo.EndY / totalVal);
                    SignalChartCtrl.Series["코스피기관"].Points.AddXY(dayIndex, organInfo.EndY / totalVal);
                    SignalChartCtrl.Series["코스피외국인"].Color = Color.Red;
                    SignalChartCtrl.Series["코스피기관"].Color = Color.Pink;
                }
                else
                {
                    totalVal = Math.Abs(foreignInfo.EndY - organInfo.EndY);
                    SignalChartCtrl.Series["코스피외국인"].Points.AddXY(dayIndex, Math.Abs(foreignInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피기관"].Points.AddXY(dayIndex, Math.Abs(organInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피외국인"].Color = Color.Red;
                    SignalChartCtrl.Series["코스피기관"].Color = Color.Blue;
                }
            }
            else
            {
                if (organInfo.EndY > 0)
                {
                    totalVal = Math.Abs(organInfo.EndY - foreignInfo.EndY);
                    SignalChartCtrl.Series["코스피외국인"].Points.AddXY(dayIndex, Math.Abs(foreignInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피기관"].Points.AddXY(dayIndex, Math.Abs(organInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피외국인"].Color = Color.Blue;
                    SignalChartCtrl.Series["코스피기관"].Color = Color.Red;
                }
                else
                {
                    totalVal = Math.Abs(foreignInfo.EndY + organInfo.EndY);
                    SignalChartCtrl.Series["코스피외국인"].Points.AddXY(dayIndex, Math.Abs(foreignInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피기관"].Points.AddXY(dayIndex, Math.Abs(organInfo.EndY) / totalVal);
                    SignalChartCtrl.Series["코스피외국인"].Color = Color.Blue;
                    SignalChartCtrl.Series["코스피기관"].Color = Color.BlueViolet;
                }
            }

            double foreignVal = Math.Abs(foreignInfo.EndY) / totalVal * 100;
            double organVal = Math.Abs(organInfo.EndY) / totalVal * 100;
            SignalChartCtrl.Series["코스피외국인"].Points[0].AxisLabel = GlobalVar.TimeToIntEx().ToString();
            SignalChartCtrl.Series["코스피외국인"].Label = "외국인" + foreignVal.ToString("F2");
            SignalChartCtrl.Series["코스피기관"].Label = "기관" + organVal.ToString("F2");
            SignalChartCtrl.ChartAreas["Volume"].AxisY.Minimum = 0;
            SignalChartCtrl.ChartAreas["Volume"].AxisY.Maximum = 100;
        }

        public SignalChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            InitControl();
        }

        private void InitChart()
        {
            SignalChartCtrl.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(211)), ((System.Byte)(223)), ((System.Byte)(240)));
            SignalChartCtrl.BackSecondaryColor = System.Drawing.Color.White;
            SignalChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            SignalChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            SignalChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            SignalChartCtrl.BorderlineWidth = 2;
            SignalChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            SignalChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.Interval = 1;
            SignalChartCtrl.ChartAreas["Price"].AxisX.Interval = 1;
            SignalChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.Interval = 25;
            SignalChartCtrl.ChartAreas["Price"].AxisY.Interval = 25;

            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.IsClustered = true;
            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.Perspective = 10;
            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.IsRightAngleAxes = false;
            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.WallWidth = 0;
            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.Inclination = 15;
            SignalChartCtrl.ChartAreas["Price"].Area3DStyle.Rotation = 10;
            SignalChartCtrl.ChartAreas["Price"].AxisX.IsLabelAutoFit = false;
            SignalChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            SignalChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = false;
            SignalChartCtrl.ChartAreas["Price"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Price"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Price"].AxisY.IsLabelAutoFit = false;
            SignalChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            SignalChartCtrl.ChartAreas["Price"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Price"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Price"].AxisY.IsStartedFromZero = false;
            SignalChartCtrl.ChartAreas["Price"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            SignalChartCtrl.ChartAreas["Price"].BackSecondaryColor = System.Drawing.Color.White;
            SignalChartCtrl.ChartAreas["Price"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            SignalChartCtrl.ChartAreas["Price"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Price"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            SignalChartCtrl.ChartAreas["Price"].Name = "Price";
            SignalChartCtrl.ChartAreas["Price"].Position.Auto = false;
            SignalChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            SignalChartCtrl.ChartAreas["Price"].Position.Width = 94F;
            SignalChartCtrl.ChartAreas["Price"].Position.X = 3F;
            SignalChartCtrl.ChartAreas["Price"].Position.Y = 3F;
            SignalChartCtrl.ChartAreas["Price"].ShadowColor = System.Drawing.Color.Transparent;
            SignalChartCtrl.ChartAreas["Volume"].AlignWithChartArea = "Price";
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.IsClustered = true;
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.Perspective = 10;
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.IsRightAngleAxes = false;
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.WallWidth = 0;
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.Inclination = 15;
            SignalChartCtrl.ChartAreas["Volume"].Area3DStyle.Rotation = 10;
            SignalChartCtrl.ChartAreas["Volume"].AxisX.IsLabelAutoFit = false;
            SignalChartCtrl.ChartAreas["Volume"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            SignalChartCtrl.ChartAreas["Volume"].AxisX.LabelStyle.IsEndLabelVisible = false;
            SignalChartCtrl.ChartAreas["Volume"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Volume"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Volume"].AxisY.IsLabelAutoFit = false;
            SignalChartCtrl.ChartAreas["Volume"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            SignalChartCtrl.ChartAreas["Volume"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Volume"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Volume"].AxisY.IsStartedFromZero = false;
            SignalChartCtrl.ChartAreas["Volume"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            SignalChartCtrl.ChartAreas["Volume"].BackSecondaryColor = System.Drawing.Color.White;
            SignalChartCtrl.ChartAreas["Volume"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            SignalChartCtrl.ChartAreas["Volume"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            SignalChartCtrl.ChartAreas["Volume"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            SignalChartCtrl.ChartAreas["Volume"].Name = "Volume";
            SignalChartCtrl.ChartAreas["Volume"].Position.Auto = false;
            SignalChartCtrl.ChartAreas["Volume"].Position.Height = 14F;
            SignalChartCtrl.ChartAreas["Volume"].Position.Width = 94F;
            SignalChartCtrl.ChartAreas["Volume"].Position.X = 3F;
            SignalChartCtrl.ChartAreas["Volume"].Position.Y = 83F;
            SignalChartCtrl.ChartAreas["Volume"].ShadowColor = System.Drawing.Color.Transparent;
        }

        private void SignalChartFrm_Load(object sender, EventArgs e)
        {
            InitChart();
            InitTimer();
        }

        private void SignalChartFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }
    }
}
